function turn_to(num){
	if(num==0){
		$('.prize').css('display','none');
		$('.list').css('display','flex');
		$('.event').css('display','none');
		$('.output').css('display','none');
		$('#add_prize').css('display','none');
		$('#add_event').css('display','none');
		$('#add_special').css('display','none');
		$('#cancel_special').css('display','none');
		$('.now').css('left','0');
		get_list();
	}
	else if(num==1){
		$('.prize').css('display','flex');
		$('.list').css('display','none');
		$('.event').css('display','none');
		$('.output').css('display','none');
		$('#add_prize').css('display','flex');
		$('#add_event').css('display','none');
		$('#add_special').css('display','none');
		$('#cancel_special').css('display','none');
		$('.now').css('left','10%');
		get_prize();
	}
	else if(num==2){
		$('.prize').css('display','none');
		$('.list').css('display','none');
		$('.event').css('display','flex');
		$('.output').css('display','none');
		$('#add_prize').css('display','none');
		$('#add_event').css('display','flex');
		if(localStorage.getItem('expected')==null){
			$('#add_special').css('display','flex');
			$('#cancel_special').css('display','none');
		}
		else{
			$('#add_special').css('display','none');
			$('#cancel_special').css('display','flex');
		}
		$('.now').css('left','20%');
		get_event();
	}
	else if(num==3){
		$('.prize').css('display','none');
		$('.list').css('display','none');
		$('.event').css('display','none');
		$('.output').css('display','flex');
		$('#add_prize').css('display','none');
		$('#add_event').css('display','none');
		$('#add_special').css('display','none');
		$('#cancel_special').css('display','none');
		$('.now').css('left','30%');
	}
	else{
		window.location.href="../index.html";
	}
}

function get_list(){
	var n=localStorage.getItem('total_of_people');
	var content="";
	var choose=function(op){
		for(var i=0;i<n;i++){
			var name=localStorage.getItem('person'+String(i));
			var state=localStorage.getItem('state'+String(i));
			if(state==null) state='未中奖';
			if(state!=op) continue;
			var str="<div class='item'>"
			str+="<button class='no'>"+String(i+1)+"</button>";
			str+="<button class='name'>"+name+"</button>";
			str+="<button class='state'>"+state+"</button>";
			str+="</div>"
			content+=str;
		}
	}
	choose('一等奖');
	choose('二等奖');
	choose('三等奖');
	choose('未中奖');
	$('.people').html(content);
}

function get_prize(){
	var n=localStorage.getItem('total_of_prize');
	var content="";
	for(var i=0;i<n;i++){
		var name=localStorage.getItem('prize'+String(i));
		var rank=localStorage.getItem('rank'+String(i));
		var amount=localStorage.getItem('amount'+String(i));
		var str="<div class='item'>";
		str+="<button class='name'>"+name+"</button>";
		str+="<button class='rank'>"+rank+"</button>";
		str+="<button class='amount'>"+amount+"</button>";
		str+="<button class='delete' onclick='del("+String(i)+")'>删除</button>";
		str+="</div>";
		content+=str;
	}
	$('.content').html(content);
}
function add(){
	$('#name').val('');
	$('#rank').val('');
	$('#amount').val('');
	$('#add_page_prize').css('display','flex');
}
function del(id){
	var n=localStorage.getItem('total_of_prize');
	for(var i=id;i<n-1;i++){
		var prize=localStorage.getItem('prize'+String(i+1));
		var rank=localStorage.getItem('rank'+String(i+1));
		var amount=localStorage.getItem('amount'+String(i+1));
		localStorage.setItem('prize'+String(i),prize);
		localStorage.setItem('rank'+String(i),rank);
		localStorage.setItem('amount'+String(i),amount);
	}
	n--;
	localStorage.removeItem('prize'+String(n));
	localStorage.removeItem('rank'+String(n));
	localStorage.removeItem('amount'+String(n));
	localStorage.setItem('total_of_prize',n);
	turn_to(1);
}

function get_event(){
	var n=localStorage.getItem('total_of_event');
	var content="";
	for(var i=1;i<=n;i++){
		for(var j=0;j<n;j++){
			var id=localStorage.getItem('event_id'+String(j));
			var rank=localStorage.getItem('event_rank'+String(j));
			var amount=localStorage.getItem('event_amount'+String(j));
			if(id==i){
				var str="<div class='item'>";
				str+="<button class='id'>"+id+"</button>";
				str+="<button class='rank'>"+rank+"</button>";
				str+="<button class='amount'>"+amount+"</button>";
				str+="<button class='delete' onclick='del_event("+String(i)+")'>删除</button>";
				str+="</div>";
				content+=str;
				break;
			}
		}
	}
	$('.event_list').html(content);
}
function add_event(){
	$('#event_id').val('');
	$('#event_rank').val('');
	$('#event_amount').val('');
	$('#add_page_event').css('display','flex');
}
function del_event(id){
	var n=localStorage.getItem('total_of_event');
	for(var i=0;i<n;i++){
		var prize=localStorage.getItem('event_id'+String(i));
		if(prize==id) pos=i;
		else if(prize>id) localStorage.setItem('event_id'+String(i),prize-1);
	}
	for(var i=pos;i<n-1;i++){
		var prize=localStorage.getItem('event_id'+String(i+1));
		var rank=localStorage.getItem('event_rank'+String(i+1));
		var amount=localStorage.getItem('event_amount'+String(i+1));
		localStorage.setItem('event_id'+String(i),prize);
		localStorage.setItem('event_rank'+String(i),rank);
		localStorage.setItem('event_amount'+String(i),amount);
	}
	n--;
	localStorage.removeItem('event_id'+String(n));
	localStorage.removeItem('event_rank'+String(n));
	localStorage.removeItem('event_amount'+String(n));
	localStorage.setItem('total_of_event',n);
	turn_to(2);
}

function enter(op){
	if(op==0){
		var n=localStorage.getItem('total_of_prize');
		var name=$('#name').val();
		var rank=$('#rank').val();
		var amount=$('#amount').val();
		localStorage.setItem('prize'+String(n),name);
		localStorage.setItem('rank'+String(n),rank);
		localStorage.setItem('amount'+String(n),amount);
		n++;
		localStorage.setItem('total_of_prize',n);
		turn_to(1);
	}
	else if(op==1){
		var n=localStorage.getItem('total_of_event');
		var id=$('#event_id').val();
		var rank=$('#event_rank').val();
		var amount=$('#event_amount').val();
		for(var i=0;i<n;i++){
			if(localStorage.getItem('event_id'+String(i))==id){
				alert('抽奖事件的次序不能重复');
				return;
			}
		}
		localStorage.setItem('event_id'+String(n),id);
		localStorage.setItem('event_rank'+String(n),rank);
		localStorage.setItem('event_amount'+String(n),amount);
		n++;
		localStorage.setItem('total_of_event',n);
		turn_to(2);
	}
	$('.add_page').css('display','none');
}
function cancel(){
	$('.add_page').css('display','none');
}

function output(){
	var content="";
	var find=function(op){
		var n=localStorage.getItem('total_of_people');
		var str=op+':';
		for(var i=0;i<n;i++){
			var name=localStorage.getItem('person'+String(i));
			var state=localStorage.getItem('state'+String(i));
			if(state==op) str+=name+'  ';
		}
		str+=';\n';
		return str;
	}
	content+=find('一等奖');
	content+=find('二等奖');
	content+=find('三等奖');
	navigator.clipboard.writeText(content);
	alert('已将结果导入到剪切板！');
}

function special(){
	var expected=prompt('请输入预计人数');
	localStorage.setItem('expected',expected);
	turn_to(2);
}
function cancel_special(){
	localStorage.removeItem('expected');
	alert('已取消比例限制！');
	turn_to(2);
}